package com.hqyj.springBoot.test.dao;

import com.hqyj.springBoot.modules.common.entity.SearchBean;
import com.hqyj.springBoot.test.entity.City;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @author YangGuo
 * @version 1.0
 * @date 2021/8/19 14:17
 */
@Mapper
@Repository
public interface CityDao {
    /**
     * 插入城市信息
     * @param city 插入的城市
     */
    @Insert("insert into test_city (city_name, local_city_name, country_id, date_created) " +
            "values (#{cityName}, #{localCityName}, #{countryId}, #{dateCreated})")
    @Options(useGeneratedKeys = true,keyColumn = "city_id",keyProperty = "cityId")
    void insertCity(City city);

    /**
     * 更新城市信息
     * @param city 更新后的城市
     */
    @Update("update test_city set city_name = #{cityName}, local_city_name = #{localCityName} " +
            "where city_id = #{cityId}")
    void updateCity(City city);

    /**
     * 删除城市信息
     * @param cityId  改城市的id
     */
    @Delete("delete from test_city where city_id = #{cityId}")
    void deleteCityByCityId(Integer cityId);

    /**
     * 通过id查一个 城市
     * @param cityId
     * @return
     */
    @Select("select * from test_city where city_id=#{cityId}")
    City selectCityByCityId(Integer cityId);

    @Select("select * from test_city where country_id=#{countryId}")
    List<City> selectCitesByCountryId(Integer countryId);

    @Select("<script>"
            + "select * from test_city "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (city_name like '%${keyword}%' or "
            + " local_city_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by city_id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<City> getCitiesBySearchBean(SearchBean searchBean);
}
